package com.lsc.record.config;

import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * api统一返回结果格式
 * @param <T>
 */
public class ApiResult<T> implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 38301218035856181L;

	private int code = HttpStatus.OK.value();

    private String msg;

    private T data;


    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResult<T> success() {
        return success(null);
    }

    public static <T> ApiResult<T> failure() {
        return failure(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<T>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
    }

    public static <T> ApiResult<T> failure(int errorCode, String errorMsg) {
        return failure(errorCode, errorMsg, null);
    }

    public static <T> ApiResult<T> failure(int code, String errorMsg, T data) {
        return new ApiResult<T>(code, errorMsg, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}